/**
 * Choicesoft.com.cn Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package com.springboot.redis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author chenxubing
 * @version $Id: RedisController.java, v 0.1 2018-09-28 下午3:25 chenxubing Exp $$
 */
@RestController
@RequestMapping(value = "/data")
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping("/redis")
    public MessageBody redis(@RequestBody JSONObject json){

        MessageBody msg = MessageBody.getMsgSuccc();
        String oper = json.getString("oper");
        String param = json.getString("param");
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();

        try{
            switch(oper){
                case "dbsize":
                    Long dbsize = redisTemplate.execute(new RedisCallback<Long>(){
                        @Override
                        public Long doInRedis(RedisConnection connection)  throws DataAccessException{
                            return connection.dbSize();
                        }
                    });
                    json.put("dbsize",dbsize);
                    break;
                case "keys":
                    Set<String> set = redisTemplate.keys(param);
                    if(null != set){
                        JSONArray ja = new JSONArray(new ArrayList<Object>(set));
                        json.put("res",ja);
                    }
                    break;
                case "get":
                    String data = valueOperations.get(param);
                    json.put("res",data);
                    break;
                case "hget":
                    String[] arr = param.split(",");
                    String key = arr[0];
                    String field = arr[1];
                    String str = hashOperations.get(key, field);
                    json.put("res",str);
                    break;
                case "del":
                    List<String> list = null;
                    Long count = redisTemplate.delete(list);
                    json.put("res",count);
                    break;
                default:
                    break;
            }
        }catch(Exception e){
            json.put("err",e);
        }
        msg.setData(json);
        return msg;
    }
}
